package com.yuexun.dhdoctor.widget.model;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.bean.model.GridSele;
import com.yuexun.dhdoctor.base.bean.model.HelpFileData;
import com.yuexun.dhdoctor.baselistener.StringListener;
import com.yuexun.dhdoctor.widget.FlowLayout;
import com.yuexun.dhdoctor.widget.pickerUtil.ModelDatePicker;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by chenss on 2017/8/1 0001.
 */

public class CreateCheckBoxView {

    Context context;
    ArrayList<HelpFileData> dtlList;//配置详情列表
    ModelDatePicker customDatePicker1;

    SimpleDateFormat sdf = null;
    String isDate;
    List<HelpFileData> alreadyDtlList;
    boolean isBackfill;

    public CreateCheckBoxView(Context context, ArrayList<HelpFileData> dtlList, List<HelpFileData> alreadyDtlList, boolean isBackfill){
        this.context = context;
        this.dtlList = dtlList;
        this.alreadyDtlList = alreadyDtlList;
        this.isBackfill = isBackfill;
    }

    /**
     * 方法描述：从布局中添加多选+文本布局
     *
     * @param fileData
     * @return
     */
    public View createCheckboxView(String[] typeData, final HelpFileData fileData, final boolean hasTxt) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        View view;
        if(typeData[1].equals("7")){
            view = LayoutInflater.from(context).inflate(R.layout.add_item_checkbox_layout_08, null);//也可以从XML中加载布局
        }else{
            view = LayoutInflater.from(context).inflate(R.layout.add_item_checkbox_layout_01, null);//也可以从XML中加载布局
        }


        view.setLayoutParams(lp);//设置布局参数

        TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
        FlowLayout viewGroup = (FlowLayout) view.findViewById(R.id.viewGroup);
        final TextView defined_title = (TextView) view.findViewById(R.id.defined_title);
        final EditText defined_txt = (EditText) view.findViewById(R.id.defined_txt);
        final LinearLayout definedView = (LinearLayout) view.findViewById(R.id.defined_view);
        TextView tvView = (TextView) view.findViewById(R.id.tv_view);

        if(typeData[0].equals("8")){
            tvView.setVisibility(View.GONE);
        }
        txt_title.setText(fileData.getDtlName());

        final HelpFileData fileD = new HelpFileData();
        fileD.setDtlName(fileData.getDtlName());
        fileD.setShowType(typeData[0]);

        final ArrayList<HelpFileData> helpFileDatas = new ArrayList<>();
        fileD.setList(helpFileDatas);
        dtlList.add(fileD);

        ArrayList<HelpFileData> cont = new ArrayList<>();
        if (isBackfill) {   //回填
            for(HelpFileData data : alreadyDtlList){
                if(data.getDtlId().equals(fileData.getDtlId())){
                    cont = data.getList();
                }
            }
        }

        final List<GridSele> list = new ArrayList<GridSele>();

        if (hasTxt) {
            definedView.setVisibility(View.VISIBLE);
            if(fileData.getList().size() == 1){
                if(typeData[1].equals("7")){
                    defined_title.setText(fileData.getList().get(0).getDtlName());

                    if(isBackfill && cont.get(0).getDtlName().equals(fileData.getList().get(0).getDtlName())){
                        defined_txt.setText(cont.get(0).getDtlValue());

                        HelpFileData f = new HelpFileData();
                        f.setDtlName(cont.get(0).getDtlName());
                        f.setDtlValue(cont.get(0).getDtlValue());
                        f.setShowType(fileData.getShowType());
                        fileD.getList().add(f);
                    }

                }else{
                    if(!fileData.getList().get(0).getDtlName().isEmpty()){
                        GridSele gridSele = new GridSele();
                        gridSele.setDtlName(fileData.getList().get(0).getDtlName());

                        if(isBackfill && cont.get(0).getDtlName().equals(fileData.getList().get(0).getDtlName())){
                            gridSele.setSelect(true);

                            HelpFileData f = new HelpFileData();
                            f.setDtlName(cont.get(0).getDtlName());
                            f.setDtlValue(cont.get(0).getDtlValue());
                            f.setShowType(fileData.getShowType());
                            fileD.getList().add(f);
                        } else {
                            gridSele.setSelect(false);
                        }

                        list.add(gridSele);
                    }
                }

            }else{
                if(typeData[1].equals("7")){
                    for (int i = 1; i < fileData.getList().size(); i++) {
                        GridSele gridSele = new GridSele();
                        gridSele.setDtlName(fileData.getList().get(i).getDtlName());

                        if(isBackfill){
                            for(HelpFileData data : cont){
                                gridSele.setSelect(false);
                                if(data.getDtlName().equals(fileData.getList().get(i).getDtlName())){
                                    gridSele.setSelect(true);

                                    HelpFileData f = new HelpFileData();
                                    f.setDtlName(data.getDtlName());
                                    f.setDtlValue(data.getDtlValue());
                                    f.setShowType(fileData.getShowType());
                                    fileD.getList().add(f);
                                    break;
                                }
                            }
                        } else {
                            gridSele.setSelect(false);
                        }

                        list.add(gridSele);
                    }
                    defined_title.setText(fileData.getList().get(0).getDtlName());

                    if(isBackfill){
                        for(HelpFileData data : cont){
                            if(data.getDtlName().equals(fileData.getList().get(0).getDtlName())){
                                defined_txt.setText(data.getDtlValue());

                                HelpFileData f = new HelpFileData();
                                f.setDtlName(data.getDtlName());
                                f.setDtlValue(data.getDtlValue());
                                f.setShowType(fileData.getShowType());
                                fileD.getList().add(f);
                                break;
                            }
                        }
                    }
                }else{
                    for (int i = 0; i < fileData.getList().size() - 1; i++) {
                        GridSele gridSele = new GridSele();
                        gridSele.setDtlName(fileData.getList().get(i).getDtlName());

                        if(isBackfill){
                            for(HelpFileData data : cont){
                                gridSele.setSelect(false);
                                if(data.getDtlName().equals(fileData.getList().get(i).getDtlName())){
                                    gridSele.setSelect(true);

                                    HelpFileData f = new HelpFileData();
                                    f.setDtlName(data.getDtlName());
                                    f.setDtlValue(data.getDtlValue());
                                    f.setShowType(fileData.getShowType());
                                    fileD.getList().add(f);
                                    break;
                                }
                            }
                        } else {
                            gridSele.setSelect(false);
                        }

                        list.add(gridSele);
                    }
                    defined_title.setText(fileData.getList().get(fileData.getList().size()-1).getDtlName());

                    if(isBackfill){
                        for(HelpFileData data : cont){
                            if(data.getDtlName().equals(fileData.getList().get(fileData.getList().size()-1).getDtlName())){
                                defined_txt.setText(data.getDtlValue());

                                HelpFileData f = new HelpFileData();
                                f.setDtlName(data.getDtlName());
                                f.setDtlValue(data.getDtlValue());
                                f.setShowType(fileData.getShowType());
                                fileD.getList().add(f);
                                break;
                            }
                        }
                    }
                }
            }
        } else {
            definedView.setVisibility(View.GONE);
            for (int i = 0; i < fileData.getList().size(); i++) {
                GridSele gridSele = new GridSele();
                gridSele.setDtlName(fileData.getList().get(i).getDtlName());

                if(isBackfill){
                    for(HelpFileData data : cont){
                        gridSele.setSelect(false);
                        if(data.getDtlName().equals(fileData.getList().get(i).getDtlName())){
                            gridSele.setSelect(true);

                            HelpFileData f = new HelpFileData();
                            f.setDtlName(data.getDtlName());
                            f.setDtlValue(data.getDtlValue());
                            f.setShowType(fileData.getShowType());
                            fileD.getList().add(f);
                            break;
                        }
                    }
                } else {
                    gridSele.setSelect(false);
                }

                list.add(gridSele);
            }
        }

        for(final GridSele sele : list){
            View view2 = LayoutInflater.from(context).inflate(R.layout.item_select_checkbox, null);
            CheckBox checkBox = (CheckBox) view2.findViewById(R.id.checkbox);
            checkBox.setText(sele.getDtlName());
            checkBox.setChecked(sele.isSelect());
            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 10, 10, 0);
            viewGroup.addView(view2, params);

            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    sele.setSelect(isChecked);

                    helpFileDatas.clear();
                    for (GridSele gridSele : list) {
                        if (gridSele.isSelect()) {
                            HelpFileData f = new HelpFileData();
                            f.setDtlName(gridSele.getDtlName());
                            f.setDtlValue(gridSele.getDtlName());
                            helpFileDatas.add(f);
                        }
                    }
                    if (!defined_txt.getText().toString().isEmpty()) {
                        HelpFileData f = new HelpFileData();
                        f.setDtlName("文本");
                        f.setDtlValue(defined_txt.getText().toString());
                        helpFileDatas.add(f);
                    }
                    fileD.setList(helpFileDatas);
                }
            });
        }

        defined_txt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!defined_txt.getText().toString().isEmpty()) {
                    boolean has = false;
                    for(HelpFileData helpFileData : helpFileDatas){
                        if(helpFileData.getDtlName().equals(defined_title.getText().toString())){
                            has = true;
                        }
                    }

                    if(has){
                        for(HelpFileData helpFileData : helpFileDatas){
                            if(helpFileData.getDtlName().equals(defined_title.getText().toString())){
                                helpFileData.setDtlValue(defined_txt.getText().toString());
                            }
                        }
                    }else{
                        HelpFileData f = new HelpFileData();
                        f.setDtlName(defined_title.getText().toString());
                        f.setDtlValue(defined_txt.getText().toString());
                        helpFileDatas.add(f);
                    }
                }else{
                    for(HelpFileData helpFileData : helpFileDatas){
                        if(helpFileData.getDtlName().equals(defined_title.getText().toString())){
                            helpFileDatas.remove(helpFileData);
                        }
                    }
                }
                fileD.setList(helpFileDatas);
            }
        });
        return view;
    }

    /**
     * 方法描述：从布局中添加多选+时间
     *
     * @param fileData
     * @return
     */
    public View createCheckbox2View(String[] typeData, final HelpFileData fileData) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        View view;
        if(typeData[1].equals("8") || typeData[1].equals("9")){
            view = LayoutInflater.from(context).inflate(R.layout.add_item_checkbox_layout_09, null);//也可以从XML中加载布局
        }else{
            view = LayoutInflater.from(context).inflate(R.layout.add_item_checkbox_layout_02, null);//也可以从XML中加载布局
        }

        view.setLayoutParams(lp);//设置布局参数

        TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
        FlowLayout viewGroup = (FlowLayout) view.findViewById(R.id.viewGroup);
        final CheckBox defined_title = (CheckBox) view.findViewById(R.id.defined_title);
        final TextView date_view = (TextView) view.findViewById(R.id.date_view);
        final LinearLayout definedView = (LinearLayout) view.findViewById(R.id.defined_view);
        TextView tvView = (TextView) view.findViewById(R.id.tv_view);

        if(typeData[0].equals("8")){
            tvView.setVisibility(View.GONE);
        }
        txt_title.setText(fileData.getDtlName());
        if(typeData[1].equals("3") || typeData[1].equals("8")){
            sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
            isDate = "3";
        }else if(typeData[1].equals("4") || typeData[1].equals("9")){
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
            isDate = "4";
        }

        final HelpFileData fileD = new HelpFileData();
        fileD.setDtlName(fileData.getDtlName());
        fileD.setShowType(typeData[0]);

        final ArrayList<HelpFileData> helpFileDatas = new ArrayList<>();
        fileD.setList(helpFileDatas);
        dtlList.add(fileD);

        ArrayList<HelpFileData> cont = new ArrayList<>();
        if (isBackfill) {   //回填
            for(HelpFileData data : alreadyDtlList){
                if(data.getDtlId().equals(fileData.getDtlId())){
                    cont = data.getList();
                }
            }
        }

        final List<GridSele> list = new ArrayList<GridSele>();

        if(fileData.getList().size() == 1){
            if(typeData[1].equals("8") || typeData[1].equals("9")){
                defined_title.setText(fileData.getList().get(0).getDtlName());

                if(isBackfill && cont.get(0).getDtlName().equals(fileData.getList().get(0).getDtlName())){
                    defined_title.setChecked(true);
                    date_view.setText(cont.get(0).getDtlValue());

                    HelpFileData f = new HelpFileData();
                    f.setDtlName(cont.get(0).getDtlName());
                    f.setDtlValue(cont.get(0).getDtlValue());
                    f.setShowType(fileData.getShowType());
                    fileD.getList().add(f);
                }
            }else{
                if(!fileData.getList().get(0).getDtlName().isEmpty()){
                    GridSele gridSele = new GridSele();
                    gridSele.setDtlName(fileData.getList().get(0).getDtlName());
                    if(isBackfill && cont.get(0).getDtlName().equals(fileData.getList().get(0).getDtlName())){
                        gridSele.setSelect(true);

                        HelpFileData f = new HelpFileData();
                        f.setDtlName(cont.get(0).getDtlName());
                        f.setDtlValue(cont.get(0).getDtlValue());
                        f.setShowType(fileData.getShowType());
                        fileD.getList().add(f);
                    } else {
                        gridSele.setSelect(false);
                    }
                    list.add(gridSele);
                }
            }
        }else{
            if(typeData[1].equals("8") || typeData[1].equals("9")){
                for (int i = 1; i < fileData.getList().size(); i++) {
                    GridSele gridSele = new GridSele();
                    gridSele.setDtlName(fileData.getList().get(i).getDtlName());
                    if(isBackfill){
                        for(HelpFileData data : cont){
                            gridSele.setSelect(false);
                            if(data.getDtlName().equals(fileData.getList().get(i).getDtlName())){
                                gridSele.setSelect(true);

                                HelpFileData f = new HelpFileData();
                                f.setDtlName(data.getDtlName());
                                f.setDtlValue(data.getDtlValue());
                                f.setShowType(fileData.getShowType());
                                fileD.getList().add(f);
                                break;
                            }
                        }
                    } else {
                        gridSele.setSelect(false);
                    }
                    list.add(gridSele);
                }
                defined_title.setText(fileData.getList().get(0).getDtlName());
                if(isBackfill){
                    for(HelpFileData data : cont){
                        if(data.getDtlName().equals(fileData.getList().get(0).getDtlName())){
                            defined_title.setChecked(true);
                            date_view.setText(data.getDtlValue());

                            HelpFileData f = new HelpFileData();
                            f.setDtlName(data.getDtlName());
                            f.setDtlValue(data.getDtlValue());
                            f.setShowType(fileData.getShowType());
                            fileD.getList().add(f);
                            break;
                        }
                    }
                }
            }else{
                for (int i = 0; i < fileData.getList().size() - 1; i++) {
                    GridSele gridSele = new GridSele();
                    gridSele.setDtlName(fileData.getList().get(i).getDtlName());
                    if(isBackfill){
                        for(HelpFileData data : cont){
                            gridSele.setSelect(false);
                            if(data.getDtlName().equals(fileData.getList().get(i).getDtlName())){
                                gridSele.setSelect(true);

                                HelpFileData f = new HelpFileData();
                                f.setDtlName(data.getDtlName());
                                f.setDtlValue(data.getDtlValue());
                                f.setShowType(fileData.getShowType());
                                fileD.getList().add(f);
                                break;
                            }
                        }
                    } else {
                        gridSele.setSelect(false);
                    }
                    list.add(gridSele);
                }
                defined_title.setText(fileData.getList().get(fileData.getList().size()-1).getDtlName());
                if(isBackfill){
                    for(HelpFileData data : cont){
                        if(data.getDtlName().equals(fileData.getList().get(fileData.getList().size()-1).getDtlName())){
                            defined_title.setChecked(true);
                            date_view.setText(data.getDtlValue());

                            HelpFileData f = new HelpFileData();
                            f.setDtlName(data.getDtlName());
                            f.setDtlValue(data.getDtlValue());
                            f.setShowType(fileData.getShowType());
                            fileD.getList().add(f);
                            break;
                        }
                    }
                }
            }
        }

        for(final GridSele sele : list){
            View view2 = LayoutInflater.from(context).inflate(R.layout.item_select_checkbox, null);
            CheckBox checkBox = (CheckBox) view2.findViewById(R.id.checkbox);
            checkBox.setText(sele.getDtlName());
            checkBox.setChecked(sele.isSelect());
            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 10, 10, 0);
            viewGroup.addView(view2, params);

            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    sele.setSelect(isChecked);
                    helpFileDatas.clear();
                    for (GridSele gridSele : list) {
                        if (gridSele.isSelect()) {
                            HelpFileData f = new HelpFileData();
                            f.setDtlName(gridSele.getDtlName());
                            f.setDtlValue(gridSele.getDtlName());
                            helpFileDatas.add(f);
                        }
                    }
                    if (!date_view.getText().toString().isEmpty()) {
                        HelpFileData f = new HelpFileData();
                        f.setDtlName(defined_title.getText().toString());
                        f.setDtlValue(date_view.getText().toString());
                        helpFileDatas.add(f);
                    }
                    fileD.setList(helpFileDatas);
                }
            });
        }

        date_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initDatePicker(date_view, isDate, new StringListener() {
                    @Override
                    public void getBack(String position) {
                        boolean has = false;
                        for(HelpFileData helpFileData : helpFileDatas){
                            if(helpFileData.getDtlName().equals(defined_title.getText().toString())){
                                has = true;
                            }
                        }
                        if(has){
                            for(HelpFileData helpFileData : helpFileDatas){
                                if(helpFileData.getDtlName().equals(defined_title.getText().toString())){
                                    helpFileData.setDtlValue(position);
                                }
                            }
                        }else{
                            if(!defined_title.isChecked()){
                                defined_title.setChecked(true);
                            }
                            HelpFileData f = new HelpFileData();
                            f.setDtlName(defined_title.getText().toString());
                            f.setDtlValue(position);
                            helpFileDatas.add(f);
                        }
                        fileD.setList(helpFileDatas);
                    }
                });
            }
        });
        defined_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(defined_title.isChecked()){
//                    definedView.setVisibility(View.VISIBLE);
                }else{
//                    definedView.setVisibility(View.GONE);
                    date_view.setText("");
                    for(HelpFileData helpFileData : helpFileDatas){
                        if(helpFileData.getDtlName().equals(defined_title.getText().toString())){
                            helpFileDatas.remove(helpFileData);
                        }
                    }
                    fileD.setList(helpFileDatas);
                }
            }
        });
        return view;
    }

    /**
     * 方法描述：从布局中添加多选+时间范围
     *
     * @param fileData
     * @return
     */
    public View createCheckbox3View(String[] typeData, final HelpFileData fileData) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        View view;
        if(typeData[1].equals("10") || typeData[1].equals("11")){
            view = LayoutInflater.from(context).inflate(R.layout.add_item_checkbox_layout_10, null);//也可以从XML中加载布局
        }else{
            view = LayoutInflater.from(context).inflate(R.layout.add_item_checkbox_layout_03, null);//也可以从XML中加载布局
        }

        view.setLayoutParams(lp);//设置布局参数

        TextView tvView = (TextView) view.findViewById(R.id.tv_view);
        TextView txt_title = (TextView) view.findViewById(R.id.txt_title);
        FlowLayout viewGroup = (FlowLayout) view.findViewById(R.id.viewGroup);
        final CheckBox defined_title = (CheckBox) view.findViewById(R.id.defined_title);
        final TextView startDate = (TextView) view.findViewById(R.id.start_date_view);
        final TextView endDate = (TextView) view.findViewById(R.id.end_date_view);
        final LinearLayout definedView = (LinearLayout) view.findViewById(R.id.defined_view);

        txt_title.setText(fileData.getDtlName());
        if(typeData[0].equals("8")){
            tvView.setVisibility(View.GONE);
        }
        if(typeData[1].equals("5") || typeData[1].equals("10")){
            sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
            isDate = "3";
        }else if(typeData[1].equals("6") || typeData[1].equals("11")){
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
            isDate = "4";
        }

        final HelpFileData fileD = new HelpFileData();
        fileD.setDtlName(fileData.getDtlName());
        fileD.setShowType(typeData[0]);

        final ArrayList<HelpFileData> helpFileDatas = new ArrayList<>();
        fileD.setList(helpFileDatas);
        dtlList.add(fileD);

        ArrayList<HelpFileData> cont = new ArrayList<>();
        if (isBackfill) {   //回填
            for(HelpFileData data : alreadyDtlList){
                if(data.getDtlId().equals(fileData.getDtlId())){
                    cont = data.getList();
                }
            }
        }

        final List<GridSele> list = new ArrayList<GridSele>();

        if(fileData.getList().size() == 1){
            if(typeData[1].equals("10") || typeData[1].equals("11")){
                defined_title.setText(fileData.getList().get(0).getDtlName());

                if(isBackfill && cont.get(0).getDtlName().equals(fileData.getList().get(0).getDtlName())){
                    defined_title.setChecked(true);
                    String[] time = cont.get(0).getDtlValue().replace("&nbsp;", " ").split(" -- ");
                    startDate.setText(time[0]);
                    endDate.setText(time[1]);

                    HelpFileData f = new HelpFileData();
                    f.setDtlName(cont.get(0).getDtlName());
                    f.setDtlValue(cont.get(0).getDtlValue());
                    f.setShowType(fileData.getShowType());
                    fileD.getList().add(f);
                }
            }else{
                if(!fileData.getList().get(0).getDtlName().isEmpty()){
                    GridSele gridSele = new GridSele();
                    gridSele.setDtlName(fileData.getList().get(0).getDtlName());
                    if(isBackfill && cont.get(0).getDtlName().equals(fileData.getList().get(0).getDtlName())){
                        gridSele.setSelect(true);

                        HelpFileData f = new HelpFileData();
                        f.setDtlName(cont.get(0).getDtlName());
                        f.setDtlValue(cont.get(0).getDtlValue());
                        f.setShowType(fileData.getShowType());
                        fileD.getList().add(f);
                    } else {
                        gridSele.setSelect(false);
                    }
                    list.add(gridSele);
                }
            }
        }else{
            if(typeData[1].equals("10") || typeData[1].equals("11")){
                for (int i = 1; i < fileData.getList().size(); i++) {
                    GridSele gridSele = new GridSele();
                    gridSele.setDtlName(fileData.getList().get(i).getDtlName());
                    if(isBackfill){
                        for(HelpFileData data : cont){
                            gridSele.setSelect(false);
                            if(data.getDtlName().equals(fileData.getList().get(i).getDtlName())){
                                gridSele.setSelect(true);

                                HelpFileData f = new HelpFileData();
                                f.setDtlName(data.getDtlName());
                                f.setDtlValue(data.getDtlValue());
                                f.setShowType(fileData.getShowType());
                                fileD.getList().add(f);
                                break;
                            }
                        }
                    } else {
                        gridSele.setSelect(false);
                    }
                    list.add(gridSele);
                }
                defined_title.setText(fileData.getList().get(0).getDtlName());
                if(isBackfill){
                    for(HelpFileData data : cont){
                        if(data.getDtlName().equals(fileData.getList().get(0).getDtlName())){
                            defined_title.setChecked(true);
                            String[] time = data.getDtlValue().replace("&nbsp;", " ").split(" -- ");
                            startDate.setText(time[0]);
                            endDate.setText(time[1]);

                            HelpFileData f = new HelpFileData();
                            f.setDtlName(data.getDtlName());
                            f.setDtlValue(data.getDtlValue());
                            f.setShowType(fileData.getShowType());
                            fileD.getList().add(f);
                            break;
                        }
                    }
                }
            }else{
                for (int i = 0; i < fileData.getList().size() - 1; i++) {
                    GridSele gridSele = new GridSele();
                    gridSele.setDtlName(fileData.getList().get(i).getDtlName());
                    if(isBackfill){
                        for(HelpFileData data : cont){
                            gridSele.setSelect(false);
                            if(data.getDtlName().equals(fileData.getList().get(i).getDtlName())){
                                gridSele.setSelect(true);

                                HelpFileData f = new HelpFileData();
                                f.setDtlName(data.getDtlName());
                                f.setDtlValue(data.getDtlValue());
                                f.setShowType(fileData.getShowType());
                                fileD.getList().add(f);
                                break;
                            }
                        }
                    } else {
                        gridSele.setSelect(false);
                    }
                    list.add(gridSele);
                }
                defined_title.setText(fileData.getList().get(fileData.getList().size()-1).getDtlName());
                if(isBackfill){
                    for(HelpFileData data : cont){
                        if(data.getDtlName().equals(fileData.getList().get(fileData.getList().size()-1).getDtlName())){
                            defined_title.setChecked(true);
                            String[] time = data.getDtlValue().replace("&nbsp;", " ").split(" -- ");
                            startDate.setText(time[0]);
                            endDate.setText(time[1]);

                            HelpFileData f = new HelpFileData();
                            f.setDtlName(data.getDtlName());
                            f.setDtlValue(data.getDtlValue());
                            f.setShowType(fileData.getShowType());
                            fileD.getList().add(f);
                            break;
                        }
                    }
                }
            }
        }

        for(final GridSele sele : list) {
            View view2 = LayoutInflater.from(context).inflate(R.layout.item_select_checkbox, null);
            CheckBox checkBox = (CheckBox) view2.findViewById(R.id.checkbox);
            checkBox.setText(sele.getDtlName());
            checkBox.setChecked(sele.isSelect());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 10, 10, 0);
            viewGroup.addView(view2, params);

            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    sele.setSelect(isChecked);
                    helpFileDatas.clear();
                    for (GridSele gridSele : list) {
                        if (gridSele.isSelect()) {
                            HelpFileData f = new HelpFileData();
                            f.setDtlName(gridSele.getDtlName());
                            f.setDtlValue(gridSele.getDtlName());
                            helpFileDatas.add(f);
                        }
                    }
                    if (!startDate.getText().toString().isEmpty() && !endDate.getText().toString().isEmpty()) {
                        HelpFileData f = new HelpFileData();
                        f.setDtlName(defined_title.getText().toString());
                        f.setDtlValue(startDate.getText().toString() + " -- " + endDate.getText().toString());
                        helpFileDatas.add(f);
                    }
                    fileD.setList(helpFileDatas);
                }
            });
        }

        startDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initDatePicker(startDate, isDate, new StringListener() {
                    @Override
                    public void getBack(String position) {
                        boolean has = false;
                        for(HelpFileData helpFileData : helpFileDatas){
                            if(helpFileData.getDtlName().equals(defined_title.getText().toString())){
                                has = true;
                            }
                        }
                        if(has){
                            for(HelpFileData helpFileData : helpFileDatas){
                                if(helpFileData.getDtlName().equals(defined_title.getText().toString())){
                                    helpFileData.setDtlValue(position + " -- " + endDate.getText().toString());
                                }
                            }
                        }else{
                            if(!defined_title.isChecked()){
                                defined_title.setChecked(true);
                            }
                            HelpFileData f = new HelpFileData();
                            f.setDtlName(defined_title.getText().toString());
                            f.setDtlValue(position + " -- " + endDate.getText().toString());
                            helpFileDatas.add(f);
                        }
                        fileD.setList(helpFileDatas);
                    }
                });
            }
        });
        endDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initDatePicker(endDate, isDate, new StringListener() {
                    @Override
                    public void getBack(String position) {
                        boolean has = false;
                        for(HelpFileData helpFileData : helpFileDatas){
                            if(helpFileData.getDtlName().equals(defined_title.getText().toString())){
                                has = true;
                            }
                        }
                        if(has){
                            for(HelpFileData helpFileData : helpFileDatas){
                                if(helpFileData.getDtlName().equals(defined_title.getText().toString())){
                                    helpFileData.setDtlValue(startDate.getText().toString() + " -- " + position);
                                }
                            }
                        }else{
                            if(!defined_title.isChecked()){
                                defined_title.setChecked(true);
                            }
                            HelpFileData f = new HelpFileData();
                            f.setDtlName(defined_title.getText().toString());
                            f.setDtlValue(startDate.getText().toString() + " -- " + position);
                            helpFileDatas.add(f);
                        }
                        fileD.setList(helpFileDatas);
                    }
                });
            }
        });
        defined_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(defined_title.isChecked()){
//                    definedView.setVisibility(View.VISIBLE);
                }else{
//                    definedView.setVisibility(View.GONE);
                    startDate.setText("");
                    endDate.setText("");
                    for(HelpFileData helpFileData : helpFileDatas){
                        if(helpFileData.getDtlName().equals(defined_title.getText().toString())){
                            helpFileDatas.remove(helpFileData);
                        }
                    }
                    fileD.setList(helpFileDatas);
                }
            }
        });
        return view;
    }

    private void initDatePicker(final TextView tv, final String num, final StringListener listener) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String now = sdf.format(new Date());

        customDatePicker1 = new ModelDatePicker(context, new ModelDatePicker.ResultHandler() {
            @Override
            public void handle(String time) { // 回调接口，获得选中的时间
                tv.setText(time);
                listener.getBack(time);
            }
        }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker1.showSpecific(num);
        customDatePicker1.setIsLoop(false); // 不允许循环滚动
        customDatePicker1.show(now);
    }

}
